<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="小区" prop="buildingId">
        <el-select v-model="queryParams.buildingId" placeholder="请选择小区" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="区域" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入区域"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车位号" prop="spotNo">
        <el-input
          v-model="queryParams.spotNo"
          placeholder="请输入车位号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车位类型" prop="spotType">
        <el-select v-model="queryParams.spotType" placeholder="请选择车位类型" clearable size="small">
          <el-option
            v-for="dict in spotTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入住户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住户手机号" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入住户手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定人" prop="bindUser">
        <el-select v-model="queryParams.bindUser" placeholder="请选择绑定人" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定日期">
        <el-date-picker
          v-model="daterangeBindTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否已绑定" prop="isBind">
        <el-select v-model="queryParams.isBind" placeholder="请选择是否已绑定" clearable size="small">
          <el-option
            v-for="dict in isBindOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park:spot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['park:spot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:spot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:spot:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="小区" align="center" prop="buildingId" />
      <el-table-column label="区域" align="center" prop="area" />
      <el-table-column label="车位号" align="center" prop="spotNo" />
      <el-table-column label="车位类型" align="center" prop="spotType" :formatter="spotTypeFormat" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="住户姓名" align="center" prop="userName" />
      <el-table-column label="住户手机号" align="center" prop="telephone" />
      <el-table-column label="车牌号" align="center" prop="plateNumber" />
      <el-table-column label="绑定人" align="center" prop="bindUser" />
      <el-table-column label="绑定日期" align="center" prop="bindTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bindTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已绑定" align="center" prop="isBind" :formatter="isBindFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['park:spot:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:spot:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="小区" prop="buildingId">
          <el-select v-model="form.buildingId" placeholder="请选择小区">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="区域" prop="area">
          <el-input v-model="form.area" placeholder="请输入区域" />
        </el-form-item>
        <el-form-item label="车位号" prop="spotNo">
          <el-input v-model="form.spotNo" placeholder="请输入车位号" />
        </el-form-item>
        <el-form-item label="车位类型" prop="spotType">
          <el-select v-model="form.spotType" placeholder="请选择车位类型">
            <el-option
              v-for="dict in spotTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="住户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入住户姓名" />
        </el-form-item>
        <el-form-item label="住户手机号" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入住户手机号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNumber">
          <el-input v-model="form.plateNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="行驶证照片">
          <imageUpload v-model="form.drivingLicense"/>
        </el-form-item>
        <el-form-item label="绑定人" prop="bindUser">
          <el-select v-model="form.bindUser" placeholder="请选择绑定人">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="绑定日期" prop="bindTime">
          <el-date-picker clearable size="small"
            v-model="form.bindTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择绑定日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否已绑定" prop="isBind">
          <el-select v-model="form.isBind" placeholder="请选择是否已绑定">
            <el-option
              v-for="dict in isBindOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpot, getSpot, delSpot, addSpot, updateSpot, exportSpot } from "@/api/park/spot";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Spot",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停车位信息表格数据
      spotList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 车位类型字典
      spotTypeOptions: [],
      // 绑定日期时间范围
      daterangeBindTime: [],
      // 是否已绑定字典
      isBindOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingId: null,
        area: null,
        spotNo: null,
        spotType: null,
        price: null,
        userName: null,
        telephone: null,
        plateNumber: null,
        bindUser: null,
        bindTime: null,
        isBind: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingId: [
          { required: true, message: "小区不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("park_sport_type").then(response => {
      this.spotTypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isBindOptions = response.data;
    });
  },
  methods: {
    /** 查询停车位信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeBindTime && '' != this.daterangeBindTime) {
        this.queryParams.params["beginBindTime"] = this.daterangeBindTime[0];
        this.queryParams.params["endBindTime"] = this.daterangeBindTime[1];
      }
      listSpot(this.queryParams).then(response => {
        this.spotList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 车位类型字典翻译
    spotTypeFormat(row, column) {
      return this.selectDictLabel(this.spotTypeOptions, row.spotType);
    },
    // 是否已绑定字典翻译
    isBindFormat(row, column) {
      return this.selectDictLabel(this.isBindOptions, row.isBind);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        buildingId: null,
        area: null,
        spotNo: null,
        spotType: null,
        price: null,
        userName: null,
        telephone: null,
        plateNumber: null,
        drivingLicense: null,
        bindUser: null,
        bindTime: null,
        isBind: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBindTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停车位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpot(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改停车位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpot(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpot(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除停车位信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSpot(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有停车位信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSpot(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
